#include "main.h"
#include "warn.h"

warn::warn(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::warn)
{
    ui->setupUi(this);
    setFixedSize(365,124);
    setWindowTitle(trUtf8("警告！"));
    timer=new QTimer();
    QObject:: connect( timer, SIGNAL(timeout()),this, SLOT(timeDone()) );
     singleshot_timer=new QTimer();
     singleshot_timer->setSingleShot(true);
     QObject:: connect( singleshot_timer, SIGNAL(timeout()),this, SLOT(singleshot_timeDone()) );
     QObject:: connect( ui->pushButton, SIGNAL(clicked()),SLOT(on_pushButton_clicked()),
                     Qt::DirectConnection );
     counter=0;
}

warn::~warn()
{}

void warn::show_text(const char* str)
{
    ui->label->setText(trUtf8(str));
    this->exec();
}

void warn::show_sound(const char* str)
{
    timer->start(500);
    ui->label->setText(trUtf8(str));
    this->exec();
}

 void warn::sound(int sec)
{
  timer->start(500);
  singleshot_timer->start(1000*sec);
}

void warn::timeDone()
{
    if( ++counter%2) ui->label_time->setNum(counter/2);
    if(SPK)  {pwm->pwm_work_on(1000);SPK_OFF LEDA_OFF SPK=0;}
    else  {pwm->pwm_stop();SPK_ON LEDA_ON SPK=1;}
}


void warn::singleshot_timeDone()
{
  timer->stop();
  SPK_OFF
  LEDA_ON
  pwm->pwm_stop();
  counter=0;
}


void warn::on_pushButton_clicked()
{
      BUTTON_SOUND;
    // this->move(800,0);//不能关闭， 监控程序
      singleshot_timeDone();
      this->close();
}

